<?php
	
	// Session
	session_start();
	
	
	// Nese leximi i faqes behet nga ndonje burim ekstern duke mos e definuar identifikatorin e librit
	if (!isset($_GET['id'])) {
		header("Location: ../comunity/index.php?spalsh=wrong");
		exit;
	}
	
	
	// Integrimi i skedave sistemore
    include('../include/functions.inc.php');
	include('../include/template.class.php');
	include('../include/book.class.php');
	
	
	// PROJECT
	$PROJECT = setProject();
	
	
	// Ndyshoret primare
	define('TPL','../include/styles/'.$PROJECT['style'].'/comunity/comments.html');
	define('FILENAME',basename($_SERVER["PHP_SELF"]));
	define('UID',$_SESSION['user_id']);
	define('ID',$_GET['id']);
	
	
	// DB CONNECT
	dbOpen("comunity");	
	
	
	// TMP CLASS
	$tmp = new Template(TPL,$PROJECT['lang']);
	
	
	// BOOK CLASS
	$book = new Book(ID);
	$book->setBook();
		
	// MENUES
	$getBook = "?id=".ID;
	$tmp->setContent("CATEGORY",$tmp->menu("category","../comunity"));
	$tmp->setContent("BOOKMENU",$tmp->menu("book",FILENAME,$getBook));
	$tmp->setContent("SECTIONS",sections($book->BOOK['sid']));
	$tmp->setContent("FOOTMENU",$tmp->menu("foot"));
	

	// ACCORDIONS
	$tmp->setContent("INFOBOOK",$book->BOOK);
	$tmp->setContent("FROMAUTHOR",$book->fromAuthor());
	$tmp->setContent("READMORE",$book->readMore());
	$tmp->setContent("NEWS",news("comunity"));
	$tmp->setContent("RANDOM",randomContent("comunity"));
	
	
	// Nese vizitori eshte i identifikuar
	if (isset($_SESSION['user_id'])) {

		// Kontrollo se a eshte libri i votuar nga ana e vizitorit
		$RATING = mysql_fetch_assoc(mysql_query("SELECT value, users FROM ratings WHERE book_id = '".ID."'"));
		$votedUsers = explode(",",$RATING['users']);
		$RATING['locked'] =  (in_array(UID,$votedUsers)) ? "true" : "false";
		
		
		// Nese libri eshte vleresuar
		if ($RATING['locked'] == "true") { 
			
			// Largo bllokun per vleresim te librit
			$tmp->setContent("RATING", array());
		}
		else 
			// Reset rating value
			$RATING['curval'] = 0;
		
		
		// Nese libri komentohet
		if (isset($_POST['comment'])) { 
			
			// Nese libri akoma nuk eshte vleresuar, 
			//kontrollo viziti a e ka vleresuar ne kete forme
			if ($RATING['locked'] == "false" && $_POST['rating'] == 0) 
				$ERROR['rating'] = "null";
			
			// Nese komenti nuk eshte shkruar 
			if ($_POST['comment'] == NULL) 
				$ERROR['comment'] = "null";
				
			$RATING['curval'] = $_POST['rating'] ;
				
			// Nese nuk ka asnje gabim
			if(empty($ERROR)) {
				
				// Arkivo komentin
				$query = mysql_query("INSERT INTO comments SET
												book_id = '".ID."',
												user_id = '".UID."',
												comment = '".$_POST['comment']."',
												date = '".$_SERVER['REQUEST_TIME']."' 
									");
				
				// Arkivo vleren e vleresuar
				if ($RATING['locked'] == "false") {
					
					// Vlera e re e llogaritur
					$value = $RATING['value'] + $_POST['rating'];
					$users = $RATING['users'].",".UID;
					
					// Aktualizo vleren e librit
					mysql_query("UPDATE ratings SET 
												value = '".$value."',
												users = '".$users."'
								 WHERE book_id = '".ID."' 
							   ");
				}
				
				// Nese komenti eshte arkivuar trego porosin
				if ($query) {
					
					// Trego SPLASH porosin per arkivim te sukseshem
					header("Location: comments.php?id=".ID."&splash=addcomment");
					exit;
				}
				else {
					
					// Trego porosin per rjetin e tejngarkuar
					header("Location: comments.php?id=".ID."&splash=busy");
					exit;
				}
			}
		}
		
		// Cakto vlerat e yjeve
		$tmp->setContent("RATING",$RATING);
		
		// Procesoi gabimet e gjeneruara
		$tmp->setForm($_POST,$ERROR);
		
		// Largo bllokun per porsin e komenteve
		$tmp->setContent("MESSAGE",array());
	}
	// Nese akoma vizitori nuk eshte identifikuar
	// Trego porosin per Login
	else {
		
		// Trego porosin ne vend te formes
		$tmp->setContent("MESSAGE",$tmp->xmlContent('requestlogin'));
		
		// Fshije bllokun e formes
		$tmp->setContent("FORM",array());
	}
	
	
	// Te gjitha komentet per librin 
	$query = mysql_query("SELECT user_id,comment,date 
						  FROM comments 
						  WHERE book_id = '".ID."' 
						  ORDER BY date DESC 
						");
	
	if (mysql_num_rows($query) > 0) {
		
		while ($ROW = mysql_fetch_assoc($query)) {
			
			$user = mysql_fetch_assoc(mysql_query("SELECT name,nickname,anonym FROM users WHERE id = '".$ROW['user_id']."' "));
			$ROW['userName'] = ($user['anonym']) ? $user['nickname'] : $user['name'];
			$ROW['date'] = setDate($ROW['date'],1);
			
			// Fusha me te dhenat e komenteve
			$COMMENTS[] = $ROW;
		}
		
		// Fshije bllokun e porosis
		$tmp->setContent("NOCOMMENTS",array());
	}
	else {
		
		// Trego porosin ne vend te formes
		$tmp->setContent("NOCOMMENTS",$tmp->xmlContent('nocomments'));
		
		// Xhvendos fushen e komenteve
		$COMMENTS = array();
	}
	
	// Komentet
	$tmp->setContent("COMMENTS",$COMMENTS);
	
	
	// LABELS
	$tmp->setLabels();
	
	
	// DISPLAY ALL						  
	echo $tmp->vorlage;
?>